﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IndentationSplitter.Tests
{
    [TestClass]
    public class SimpleTreeTests
    {
        protected class MockTreeNode 
        {
            public MockTreeNode(string name)
            {
                Name = name;
            }

            public string Name { get; set; }
            public MockTreeNode[] Items { get; set; }
        }

        [TestMethod, TestCategory("UnitTest")]
        public void GivenSimpleTypeCanBuildTree()
        {
            const string structure = 
@"item1
    item1.1
    item1.2
    item1.3
        item1.3.1
    item1.4
        item1.4.1
item2
    item2.1"; ;

            var items = SimpleTree.Parse<MockTreeNode>(structure, (name, children) => new MockTreeNode (name) { Items = children.ToArray()}).ToArray();

            Assert.AreEqual(2, items.Length);
            Assert.AreEqual("item1.1", items[0].Items[0].Name);

            Assert.AreEqual(4, items[0].Items.Length);
            Assert.AreEqual("item1.3", items[0].Items[2].Name);
            Assert.AreEqual("item1.3.1", items[0].Items[2].Items[0].Name);
        }

        [TestMethod, TestCategory("UnitTest")]
        public void CanAssembleTree()
        {
var items = new[]
    {
        new MockTreeNode("item1")
            {
                Items = new MockTreeNode[]
                    {
                        new MockTreeNode("item1.1"),
                        new MockTreeNode("item1.2")
                            {
                                Items = new MockTreeNode[]
                                    {
                                        new MockTreeNode("item1.2.1"),
                                        new MockTreeNode("item1.2.2"),
                                    }
                            },
                        new MockTreeNode("item1.3"),
                    }
            },
        new MockTreeNode("item2")
            {
                Items = new MockTreeNode[]
                    {
                        new MockTreeNode("item2.1"),
                        new MockTreeNode("item2.2")
                    }
            }
    };

            var actual = SimpleTree.Format(items, i => i.Name, i => i.Items, "    ");

            const string expected = @"item1
    item1.1
    item1.2
        item1.2.1
        item1.2.2
    item1.3
item2
    item2.1
    item2.2";

            Assert.AreEqual(expected, actual);
        }
    }
}
